var app = getApp()
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

var Promise = require('bluebird.min.js')  //我用了bluebird.js

function wxPromisify(fn) {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}

/**
 * 处理request请求错误
 */
function loadRequestFail(obj) {
  wx.showModal({
    title: '网络请求错误',
    content: obj.errMsg,
  })
}

function loadFail(res) {
  if (res.data.code == 1) {
    wx.showToast({
      title: '操作失败',
      duration: 2000,
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  } else if (res.data.code == 1001 || res.data.code == 1002) {
    wx.navigateTo({
      url: '../login/login',
    })
  } else {
    wx.showModal({
      title: '出错啦',
      content: res.data.msg,
    })
    console.log(res)
  }
}

module.exports = {
  wxPromisify: wxPromisify,
  formatTime: formatTime,
  loadFail: loadFail,
  loadRequestFail: loadRequestFail
}







